<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script>
        'use strict'
        // 方法1
        let xf1 = {
            name: '小范',
            birth: 1991,
            // 方法
            age: function () {
                let now = new Date().getFullYear();
                // this 指向调用它的对象
                return now - this.birth;
            }
        }

        // 方法2
        function getAge() {
            let now = new Date().getFullYear();
            // this 指向调用它的对象
            return now - this.birth;
        }

        let xf2 = {
            name: '小范儿',
            birth: 2000,
            age: getAge
        }

        // xf2.age()    ok
        // getAge()     error
        // apply 改变了getAge方法中this的指向
        let result1 = getAge.apply(xf1, []);
        let result2 = getAge.apply(xf2, []);
        console.log(result1, result2)

    </script>
</head>
<body>

</body>
</html>